import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2023-01-12
 * Time: 17:33
 */
@MultipartConfig
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求数据当中指定name名称的文件
        Part part =  req.getPart("MyFile");
        //获取文件名
        System.out.println(part.getSubmittedFileName());
        //获取文件类型
        System.out.println(part.getContentType());
        //获取文件大小
        System.out.println(part.getSize());
        //把获取的文件存储到磁盘,注意参数是路径加上新文件名称
        part.write("D:\\JAVA语言\\2022年\\2023.1.12\\src\\main\\Webapp\\456.jpg");
        resp.getWriter().write("upload ok");
    }
}
